/***************************************************************************
 *   Copyright (C) 2007 by Leonid Krashenko   *
 *   Leonid.Krashenko@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "todoitem.h"
#include <qlistview.h>
#include <qobject.h>
#include <qmessagebox.h>

bool ToDoItem::stateChanging = false;

ToDoItem::ToDoItem(QListView* parent, const QString& name)
 : QCheckListItem(parent, name, QCheckListItem::CheckBox)
{
	_name = name;
	init();
}

ToDoItem::ToDoItem(QListViewItem* parent, const QString& name)
	:QCheckListItem(parent, name, QCheckListItem::CheckBox)
{
	_name = name;
	
	init();
}
		
void ToDoItem::init()
{
	_priority = 0;
	_percentComplete = 0;
	
	setDragEnabled(TRUE);
	setDropEnabled(TRUE);
	//setSelectable(TRUE);
//	setEnabled(TRUE);
	
	updateToRoot();
}

// void ToDoItem::toggle()
// {
// 	_completed = !_completed;
// 	updateToRoot();
// }

void ToDoItem::update()
{
	setText(0, _name);
	setText(1, QString::number(_percentComplete));
	setText(2, QString::number(_priority, 'f', 2));
}

void ToDoItem::updateToRoot()
{
	update();
	
	ToDoItem* parentItem = (ToDoItem*)parent();
	
	if(_percentComplete == 100)
		setOn(true);
	else 
		setOn(false);
		
	if(parentItem)
	{
		parentItem->calculateParams();
		parentItem->updateToRoot();
	}
}
		
void ToDoItem::setPercentComplete(int amount)
{
	if(amount < 0 || amount > 100)
		return;
	
	_percentComplete = amount;
	
	updateToRoot();
}

void ToDoItem::increaseCompletion(int amount)
{
	if(childCount() != 0)
		return;
	
	if(_percentComplete+amount >= 100){
		_percentComplete = 100;
		setState(On);
	}
	else
		_percentComplete+=amount;
	
	updateToRoot();
}

void ToDoItem::decreaseCompletion(int amount)
{
	if(childCount() != 0)
		return;
	
	if(_percentComplete-amount < 0)
		_percentComplete = 0;
	else
		_percentComplete-=amount;
	
//	stateChanging = true;
	setState(Off);
	
	updateToRoot();
//	stateChanging = false;
}

void ToDoItem::increasePriority(int amount)
{
/*	if(childCount() != 0)
		return;*/
	_priority++;
	update();
//	updateToRoot();
}

void ToDoItem::decreasePriority(int amount)
{
/*	if(childCount() != 0)
		return;*/
	
	_priority--;
	update();
}

void ToDoItem::setName(const QString& s)
{
	_name = s;
	updateToRoot();
}

void ToDoItem::setPriority(int priority)
{
	_priority = priority;
	updateToRoot();
}

int ToDoItem::getPriority() const
{
	return _priority;
}

int ToDoItem::getPercentComplete() const
{
	return _percentComplete;
}

QString ToDoItem::getName() const 
{
	return _name;
}

QString ToDoItem::getContent() const 
{
	return _content;
}

void ToDoItem::setContent(const QString& text)
{
	_content = text;
}

void ToDoItem::stateChange(bool state)
{
	if(stateChanging)
		return;
	
	stateChanging = true;
	
	ToggleState s = this->state();
	
/*	if(s != NoChange){
		if(childCount()!=0){*/
			if(s == Off)
				completeChildren(false);
			else 
				completeChildren(true);
// 		}
		updateToRoot();
// 	}
	
	QCheckListItem::stateChange(state);
	
	stateChanging = false;
}

// void ToDoItem::activate()
// {
// }

void ToDoItem::toggle()
{
//	updateToRoot();
	if(isOn())
		completeChildren(true);
	else
		completeChildren(false);
	
	updateToRoot();
}

void ToDoItem::paintCell ( QPainter * p, const QColorGroup & cg, 
	int column, int width, int align )
{
	QColorGroup _cg (cg);
//	QColor c = _cg.base();
	
	if(isOn()){
		_cg.setColor(QColorGroup::Text, Qt::gray);
	}
	else
	{
		// priority colors
		QColor color;
		// red area
		if(_priority >= 0)
		{
			if(_priority <= 10)
				color.setRgb(255-_priority*255/10,255,255);
			else 
				color.setRgb(0,255,255);
		}
		// blue area
		//else if(_priority >= -10 && _priority < 0)
		else if(_priority < 0)
		{
			if(_priority >= 10 )
				color.setRgb(255,255,255+_priority*255/10);
			else 
				color.setRgb(255,255,0);
		}
		else {
			// uncolored area
			color.setRgb(255,255,255);
		}
		
		//_cg.setColor(QColorGroup::Text, color);
		_cg.setColor(QColorGroup::Base, color);
	}
	
	QCheckListItem::paintCell(p, _cg, column, width, align); 
	
	//_cg.setColor(QColorGroup::Base, c);
	//	_cg.setColor( QColorGroup::Base, QColor(255,255,255) );
}

void ToDoItem::completeChildren(bool newStatus)
{
	//todo : try to reimplement something like setState
	// todo: implement old percent memory
	(newStatus)? setOn(true) : setOn(false);
	(newStatus)?_percentComplete = 100:_percentComplete=0;
	QListViewItem* i = firstChild();
	while(i!=0)
	{
// 		(newStatus)?setEnabled(On):setEnabled(Off);
		ToDoItem* todoItem = (ToDoItem*)i;
		todoItem->completeChildren(newStatus);
		i = i->nextSibling();
	}
	update();
}

void ToDoItem::calculateParams()
{
	unsigned sum_pc=0, n = childCount();
// 	float sum_prior=0;
	
	if(!n)
		return ;
	
	QListViewItem* child = firstChild();
	
	int activeChildren = 0;
	
	while(child != 0)
	{
		ToDoItem* tmp = (ToDoItem*)child;
				
		if(!tmp->isOn()){
// 			sum_prior += tmp->getPriority();
			sum_pc += tmp->getPercentComplete();
			activeChildren++;
		}
		else
			sum_pc += 100;
		
		child = child->nextSibling();
	}
	
	float percentage = sum_pc / n;
	float priority=0;
/*	if(activeChildren!=0)
		priority = sum_prior / activeChildren;*/
	_percentComplete = percentage;
// 	_priority = priority;
//	setPercentComplete(percentage);
//	setPriority(priority);
}
